﻿using System;
using System.Collections.Generic;

using SourceGrid;

namespace iPdf
{
    public class TataGridColumn<ItemType> : SourceGrid.Grid.GridColumn
    {
        #region Fields
        public string Name;
        public string Description;
        public bool Sortable;

        public delegate void ColumnCreator<T>(T item);
        private ColumnCreator<ItemType> Creator;
        public delegate bool ColumnUpdator(int rowId);
        private ColumnUpdator Updator;

        #endregion

        #region Properties
        public SourceGrid.Grid Owner
        {
            get
            {
                return (SourceGrid.Grid)this.Grid;
            }
        }

        public SourceGrid.Cells.Cell this[int row]
        {
            get
            {
                return (SourceGrid.Cells.Cell)Owner[row, Index];
            }
            set
            {
                Owner[row, Index] = value;
            }
        }
        #endregion

        #region Ctor
        public TataGridColumn(Grid grid, ColumnCreator<ItemType> creator, 
            ColumnUpdator updator)
            : base(grid)
        {
            Creator = creator;
            Updator = updator;
        }

        #endregion

        #region Methods
        public bool Update(int row)
        {
            return Updator == null ? true : Updator(row);
        }

        public void Create(ItemType item)
        {
            if (Creator != null)
            {
                Creator(item);
            }
        }

        public object GetValueAt(int row)
        {
            if (row <= 0)
            {
                return null;
            }

            SourceGrid.Cells.Cell cell = this[row];
            if (cell == null)
            {
                return null;
            }

            return cell.Value;
        }

        public void SetValueAt(int row, object value)
        {
            if (row <= 0 || this[row] == null)
            {
                return;
            }

            this[row].Value = value;
        }

        public bool ExistsAt(int row)
        {
            return this[row] != null;
        }

        #endregion
    }
}
